/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.learn.learn_view.model_view.command;

import java.awt.Color;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.JFrame;

import br.ufmg.dees.insane.model.disc.element.Element;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.ui.rich.learn.learn_controller.Controller;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.CmdButton;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnInterface;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.command.Command;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.dialog.MatrixViewTableDialog;
import br.ufmg.dees.insane.util.IVector;

/**
*
* @author Moreira, Renata & Pitangueira, Roque
* @since May 2005
*/
public class SetModelForcesCommand implements Command {
	
	private LearnInterface parent;
	private FemModel fmodel;
	private CmdButton[][] f;
	private CmdButton[][] fmGButtons;

	
	public SetModelForcesCommand(JFrame parent) {
		this.parent = (LearnInterface)parent;
		
	}
	
	
	/** Execute method. */
	public void execute() {
		
		//Driver
		fmodel = parent.getView().getController().getModel();	
		((Controller)parent.getView().getController()).getDriver().reduceEquationsVector();
		
		//Number Format
		DecimalFormat fmt = new DecimalFormat();
        DecimalFormatSymbols fsym = new DecimalFormatSymbols();
        fsym.setDecimalSeparator('.');
        fmt.setDecimalFormatSymbols(fsym);
        fmt.applyPattern("0.000E00");
        
		//Create the Stiffness Matrix for each element
        MatrixViewTableDialog dlg = new MatrixViewTableDialog("Element Global Forces Vector", ((Element)fmodel.getElementsList().get(0)).globalEquivalentForceVector().getSize());
		ArrayList sel = this.parent.getView().getController().getSelection();
		if(sel.size()==0) parent.getView().getController().selectAll();
		ListIterator selLI = sel.listIterator();
		int c = 1;
		int size = sel.size();
		while(selLI.hasNext()){
			Element elm = (Element)selLI.next();
			IVector gf = elm.globalEquivalentForceVector();
			f = new CmdButton[gf.getSize()][1];
			Color cl = new Color(255*c/size, 255-(255*c/size), 140*c/size);
			for(int i =0; i< gf.getSize(); i++)
				for(int j =0; j<1; j++){
				f [i][j]= new CmdButton(String.valueOf(fmt.format(gf.getElement(i))));
				f [i][j].setBackground(cl);
				f[i][j].setToolTipText("Equation ["+elm.getReducedEquation(i)+"][1] of the element "+elm.getLabel()+"  Forces Vector");
    			
				}
			String[] colNames = new String[1];
			for(int i=0; i<1; i++)
				colNames[i] = "Forces";	
			
			String[] lineNames = new String[gf.getSize()];
			for(int l=0; l<gf.getSize(); l++)
				lineNames[l] = ""+elm.getReducedEquation(l);
			
			dlg.addOutputTable(elm.getLabel(),lineNames, colNames,f);
			c++;
		}		
		
		//Create the Stiffness Matrix for model
		((Controller)parent.getView().getController()).getDriver().reduceEquationsVector();
		IVector fm = ((Controller)parent.getView().getController()).getDriver().fullEquivalentForceVector();
		MatrixViewTableDialog dlg1 = new MatrixViewTableDialog("Model Global Forces Vector", fm.getSize());
		fmGButtons= new CmdButton[fm.getSize()][1];
		int [] eqs = ((Controller)parent.getView().getController()).getDriver().getNodeEquations();
		
		String[] colNames = new String[1];
		for(int k=0; k<1; k++)
			colNames[k] = "Forces";	
		
		String[] lineNames = new String[fm.getSize()];
		for(int k=0; k< eqs.length; k++){
			lineNames[k] = ""+eqs[k];}
		
		for(int i=0; i<fm.getSize(); i++)
		    for(int j=0; j<1; j++){
		        fmGButtons[i][j] = new CmdButton(String.valueOf(fmt.format(fm.getElement(i)))); 
		        fmGButtons[i][j].setBackground(Color.YELLOW);
		        fmGButtons[i][j].setCommand(new ForcesDecompositionCommand(i, j, dlg, dlg1));
		        fmGButtons[i][j].addActionListener(parent);
		        fmGButtons[i][j].setToolTipText("Decompose the element on the Equation ["+eqs[i]+"][1] of the model Forces Vector");
		    }
					
		
		dlg1.addOutputTable("Model",lineNames,colNames,fmGButtons);
		
		//Dialog
		int par = fm.getSize();
		String model= "Model Complete Forces";
		String elm=  "Element Complete Forces";	
		ModelFrame f = new ModelFrame(par, dlg1, model, dlg, elm);
		f.setVisible(true);
		
		
	}
	
	
//*****************************************************************************
	
	/** Impossible to undo - not implemented. */
	public void undo() {
	}
	
//*****************************************************************************
	
	/** Impossible to redo - not implemented. */
	public void redo() {
	}
	
//*****************************************************************************
	

	

}
